<template>
  <view>
    <view class="template-screen tn-safe-area-inset-bottom">
      <!-- 顶部自定义导航 -->
      <tn-nav-bar :isBack="false" :bottomShadow="false" backgroundColor="none">
        <view class="custom-nav tn-flex tn-flex-col-center tn-flex-row-left" @tap.stop="goBack">
          <!-- 返回按钮 -->
          <view class="custom-nav__back">
            <view class="tn-icon-backspace tn-color-white" style="font-size: 60rpx;"></view>
          </view>
          <!-- 字 -->
          <view class="custom-nav__search tn-flex tn-flex-col-center tn-flex-row-center ">
            <view class="custom-nav__search__box tn-flex tn-flex-col-center tn-flex-row-center tn-color-white">
              <text class="tn-text-bold tn-text-xl">{{ title }}</text>
            </view>
          </view>
        </view>

      </tn-nav-bar>


      <!-- 页面内容 -->
      <view class="bg-contaniner">

      </view>


      <!-- 页面内容 -->
      <view class="grain-container">
        <view class="grain-common">
          <uni-section title="选择谷子图片" type="line">
            <view>
              <uni-file-picker limit="1"
                               file-mediatype="image"
                               v-model="grainImage"
                               @select="uploadImage"

              ></uni-file-picker>
            </view>
          </uni-section>
        </view>

        <view class="grain-common">
          <uni-section title="输入谷子名称" type="line">
            <view>
              <input type="text" placeholder="请输入谷子名称" v-model="grainName"/>
            </view>
          </uni-section>
        </view>

        <view class="grain-common">
          <uni-section title="输入谷子价格" type="line">
            <view>
              <input type="digit" placeholder="请输入谷子价格" v-model="grainPrice"/>
            </view>
          </uni-section>
        </view>

        <view class="grain-common">
          <uni-section title="输入谷子数量" type="line">
            <view>
              <input type="number" placeholder="请输入谷子数量" v-model="grainQuantity"/>
            </view>
          </uni-section>
        </view>

        <view class="grain-common">
          <uni-section title="输入谷子备注" type="line">
            <view>
              <input type="text" placeholder="请输入谷子备注" v-model="otherNotes"/>
            </view>
          </uni-section>
        </view>

        <view class="grain-common">
          <uni-section title="选择谷子分组" type="line">
            <view style="width: 50%">
              <uni-data-select placeholder="请选择谷子分组" :localdata="groupTree" v-model="selectGroupId"
                               :clear="false"
              >
              </uni-data-select>
            </view>
          </uni-section>
        </view>

        <view class="grain-common">
          <uni-section title="谷子购买时间" type="line">
            <view style="width: 50%">
              <uni-datetime-picker type="datetime" v-model="purchaseTime"/>
            </view>
          </uni-section>
        </view>


        <view class="grain-bottom">
          <button type="default" class="my-button" @click="addNew">提交</button>
        </view>
      </view>


    </view>


  </view>


</template>

<script>
import template_page_mixin from "@/libs/mixin/template_page_mixin";
import TnNavBar from "@/tuniao-ui/components/tn-nav-bar/tn-nav-bar.vue";
import uniSection from "@/uni_modules/uni-section/components/uni-section/uni-section.vue";
import uniFilePicker from "@/uni_modules/uni-file-picker/components/uni-file-picker/uni-file-picker.vue";
import {addOne, editOne, uploadFile} from "@/api/home";
import {groupList} from "@/api/group";
import UniDataSelect from "@/uni_modules/uni-data-select/components/uni-data-select/uni-data-select.vue";
import uniDatetimePicker
  from "@/uni_modules/uni-datetime-picker/components/uni-datetime-picker/uni-datetime-picker.vue";

export default {
  name: 'addNew',
  components: {uniDatetimePicker, UniDataSelect, uniSection, TnNavBar, uniFilePicker},
  mixins: [template_page_mixin],
  data() {
    return {
      title: '添加谷子',
      grainId: '',
      grainName: '',
      grainPrice: '',
      grainQuantity: '',
      otherNotes: '',

      imageFile: '',
      selectGroupId: '',
      purchaseTime: '',
      groupTree: [],
      grainImage: [],
    };
  },
  onLoad() {
    // 页面加载完成
    let that = this
    this.page = this.getOpenerEventChannel();
    // 你可以在这里直接接受，这里不是$on
    this.page.on('editGrain',(item)=>{
      // 这是data 就是上个页面传递的对象了
      const data = JSON.parse(item)
      console.log('页面接受到了数据', data)
      that.title = '编辑谷子'
      that.grainName = data.name
      that.grainPrice = data.price
      that.grainQuantity = data.quantity
      that.otherNotes = data.otherNotes
      that.selectGroupId = data.groupId
      that.purchaseTime =  data.purchaseTime;//完成数据回显
      const grainImageItem = [{
        "name": "grainImage",
        "extname": "png",
        "url": data.image,
      }]
      that.grainImage = grainImageItem
      that.grainId = data.id
    });
  },
  onReady() {


  },
  methods: {
    addNew() {
      const userInfo = uni.getStorageSync('userInfo')
      const param = {
        name: this.grainName,
        price: this.grainPrice,
        quantity: this.grainQuantity,
        otherNotes: this.otherNotes,
        groupId: this.selectGroupId,
        purchaseTime: this.purchaseTime,
        image: this.grainImage[0].url,
        userId: userInfo.id
      }
      if (this.grainId !== '') {
        param.id = this.grainId
        if (this.checkParam(param)) {
          editOne(param).then(
              res => {
                console.log('编辑谷子成功', res)
                setTimeout(() => {
                      uni.showToast({
                        title: '编辑谷子成功',
                        icon: 'success'
                      })
                    }, 200
                )
                uni.switchTab({
                  url: '/pages/index/index'
                })
              }
          )
        } else {
          console.log('参数错误')
        }
      } else if (this.checkParam(param)) {
        addOne(param).then(
            res => {
              console.log('添加谷子成功', res)
              setTimeout(() => {
                    uni.showToast({
                      title: '添加谷子成功',
                      icon: 'success'
                    })
                  }, 1000
              )

              uni.navigateBack()
            }
        )
      } else {
        console.log('参数错误')
      }

    },
    checkParam(param) {
      console.log('检查参数', param)
      if (!param.name) {
        uni.showToast({
          title: '请输入谷子名称',
          icon: 'none'
        })
        return false
      }
      if (!param.price) {
        uni.showToast({
          title: '请输入谷子价格',
          icon: 'none'
        })
        return false
      }
      if (!param.quantity) {
        uni.showToast({
          title: '请输入谷子数量',
          icon: 'none'
        })
        return false
      }
      if (!param.groupId) {
        uni.showToast({
          title: '请选择谷子分组',
          icon: 'none'
        })
        return false
      }
      if (!param.purchaseTime) {
        uni.showToast({
          title: '请选择谷子购买时间',
          icon: 'none'
        })
        return false
      }
      if (!param.image) {
        uni.showToast({
          title: '请选择谷子图片',
          icon: 'none'
        })
        return false
      }
      return true

    },
    uploadImage(e) {
      console.log('上传图片', e)
      this.grainImage = e.tempFiles
      this.imageFile = e.tempFiles[0].url
      //上传图片到又拍云
      uploadFile(this.imageFile).then(res => {
        console.log('上传图片成功', res)
        this.grainImage[0].url = res.data
      })
    },
    goBack() {
      uni.navigateBack()
    }
  },
  mounted() {
    groupList().then(res => {
      console.log('获取分组列表', res)
      this.groupTree = res.data
    })
  },
}
</script>

<style lang="scss" scoped>
@import '@/static/css/templatePage/custom_nav_bar.scss';


.grain-container {
  display: flex;
  flex-direction: column;
  width: 750rpx;
  height: calc(100vh-200rpx);
  background-color: white;
  margin-top: 200rpx;

  .grain-common {
    width: 100%;
    height: 100%;
  }

  .grain-bottom {
    margin-top: 30rpx;
    width: 100%;
    height: 50%;
    display: flex;
    justify-content: center;

    .my-button {
      width: 90%;
      border-radius: 8rpx;
    }
  }
}

/* 自定义导航栏内容 start */
.custom-nav {
  height: 100%;

  &__back {
    margin: auto 5rpx;
    font-size: 40rpx;
    margin-right: 10rpx;
    margin-left: 30rpx;
    flex-basis: 5%;
  }

  &__search {
    flex-basis: 71%;
    width: 100%;
    height: 100%;

    &__box {
      width: 100%;
      height: 70%;
      padding: 10rpx 0;
      margin: 0 30rpx;
      border-radius: 60rpx 60rpx 60rpx 0;
      font-size: 24rpx;
      // background-color: rgba(255,255,255,0.1);
    }

    &__icon {
      padding-right: 10rpx;
      margin-left: 20rpx;
      font-size: 30rpx;
    }

    &__text {
    }
  }
}

/* 自定义导航栏内容 end */

.screen-shadow {
  box-shadow: 0rpx 0rpx 80rpx 0rpx rgba(0, 0, 0, 0.07);
  border-radius: 20rpx;
}

/* 图标容器12 start */
.icon12 {
  &__item {
    width: 30%;
    background-color: #FFFFFF;
    border-radius: 10rpx;
    padding: 30rpx;
    margin: 20rpx 10rpx;
    transform: scale(1);
    transition: transform 0.3s linear;
    transform-origin: center center;

    &--icon {
      width: 100rpx;
      height: 100rpx;
      font-size: 60rpx;
      border-radius: 50%;
      margin-bottom: 18rpx;
      position: relative;
      z-index: 1;

      &::after {
        content: " ";
        position: absolute;
        z-index: -1;
        width: 100%;
        height: 100%;
        left: 0;
        bottom: 0;
        border-radius: inherit;
        opacity: 1;
        transform: scale(1, 1);
        background-size: 100% 100%;
        background-image: url(https://resource.tuniaokj.com/images/cool_bg_image/icon_bg.png);


      }
    }
  }
}


/* 文章内容 start*/
.tn-blogger-content {
  &__wrap {
    box-shadow: 0rpx 0rpx 80rpx 0rpx rgba(0, 0, 0, 0.09);
    border-radius: 20rpx;
    margin: 30rpx;
  }

  &__info {
    &__btn {
      margin-right: -12rpx;
      opacity: 0.5;
    }
  }

  &__label {
    &__item {
      line-height: 45rpx;
      padding: 0 20rpx;
      margin: 5rpx 18rpx 0 0;

      &--prefix {
        color: #00FFC8;
        padding-right: 10rpx;
      }
    }

    &__desc {
      line-height: 55rpx;
    }
  }

  &__main-image {
    border-radius: 16rpx 16rpx 0 0;

    &--1 {
      max-width: 690rpx;
      min-width: 690rpx;
      max-height: 400rpx;
      min-height: 400rpx;
    }

    &--2 {
      max-width: 260rpx;
      max-height: 260rpx;
    }

    &--3 {
      height: 212rpx;
      width: 100%;
    }
  }

  &__count-icon {
    font-size: 40rpx;
    padding-right: 5rpx;
  }
}

.image-qrcode {
  padding: 180rpx 0rpx;
  font-size: 40rpx;
  font-weight: 300;
  position: relative;
}

.image-pic {
  background-size: cover;
  background-repeat: no-repeat;
  // background-attachment:fixed;
  background-position: top;
  border-radius: 10rpx;
}

/* 文章内容 end*/

/* 移动背景部分 start*/
.bg-contaniner {
  position: fixed;
  top: 0rpx;
  left: 0rpx;
  --text-color: hsl(0 95% 60%);
  --bg-color: hsl(0 0% 100%);
  --bg-size: 750rpx;
  height: 100%;
  display: grid;
  place-items: center;
  place-content: center;
  overflow: hidden;
  background-color: #FF71D2;
  z-index: -1;
}

.bg-contaniner::before {
  --size: 150vmax;

  grid-area: body;
  content: "";
  inline-size: var(--size);
  block-size: var(--size);
  background-image: url("https://resource.tuniaokj.com/images/animate/health.png");
  background-size: var(--bg-size);
  background-repeat: repeat;
  transform: rotate(0deg);
  opacity: 0.15;
  animation: bg 6s linear infinite;
}

@media (prefers-reduced-motion: reduce) {
  .bg-contaniner::before {
    animation-duration: 0s;
  }
}

@keyframes bg {
  to {
    background-position: 0 calc(var(--bg-size) * -1);
  }
}

/* 移动背景部分 end*/

/* 底部tabbar start*/
.footerfixed {
  position: fixed;
  width: 100%;
  bottom: 0;
  z-index: 999;
  background-color: #FFFFFF;
  box-shadow: 0rpx 0rpx 30rpx 0rpx rgba(0, 0, 0, 0.07);
}

.tabbar {
  display: flex;
  align-items: center;
  min-height: 110rpx;
  justify-content: space-between;
  padding: 0;
  height: calc(110rpx + env(safe-area-inset-bottom) / 2);
  padding-bottom: calc(env(safe-area-inset-bottom) / 2);
}

.tabbar .action {
  font-size: 22rpx;
  position: relative;
  flex: 1;
  text-align: center;
  padding: 0;
  display: block;
  height: auto;
  line-height: 1;
  margin: 0;
  overflow: initial;
}

.tabbar .action .bar-icon {
  width: 100rpx;
  position: relative;
  display: block;
  height: auto;
  margin: 0 auto 10rpx;
  text-align: center;
  font-size: 42rpx;
  // line-height: 50rpx;
}

.tabbar .action .bar-icon image {
  width: 50rpx;
  height: 50rpx;
  display: inline-block;
}

.tabbar .action .bar-circle {
  position: relative;
  display: block;
  margin: -60rpx auto 20rpx;
  text-align: center;
  font-size: 52rpx;
  line-height: 90rpx;
  background-color: #01BEFF;
  width: 90rpx !important;
  height: 90rpx !important;
  overflow: hidden;
  border-radius: 50%;
  box-shadow: 0rpx 0rpx 20rpx 0rpx rgba(1, 190, 255, 0.5);
}

.tabbar .action .bar-circle image {
  width: 60rpx;
  height: 60rpx;
  display: inline-block;
  margin: 15rpx auto 15rpx;
}
</style>
